package bTree.LeetCode;

import java.util.ArrayList;
import java.util.List;

public class 所有可能的路径 {
    List<List<Integer>> res = new ArrayList<>();

    public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
        List<Integer> path = new ArrayList<>();
        path.add(0);
        backTracking(path, graph, 0);
        return res;
    }

    private void backTracking(List<Integer> path, int[][] graph, int index) {
        int[] ints = graph[index];
        if (ints.length == 0) {
            return;
        }
        for (int i = 0; i < ints.length; i++) {
            path.add(ints[i]);
            if (ints[i] == graph.length - 1) {
                res.add(new ArrayList<>(path));
                path.remove(path.size() - 1);
            } else {
                backTracking(path, graph, ints[i]);
                path.remove(path.size() - 1);
            }

        }
    }
}